const {
  $util,
} = getApp();
Page({
  data: {
    cid: 0,
    list: [],
    hasMore: true,
    page: 1,
    imgUrl: $util.getImgUrl(),
  },
  onLoad(ops) {
    wx.setNavigationBarTitle({
      title: ops.barTitle && decodeURIComponent(ops.barTitle) || '网络课程',
    });
  },
  onShow() {
    this.loadNav();
  },
  changeNav(e) {
    this.setData({
      cid: e.detail.obj.id,
      page: 1,
      isLoad: false,
    }, () => {
      this.loadDetail();
    });
  },
  /**
   * 课程列表
   */
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/edu.train.getlesson',
      data: {
        cid: this.data.cid,
        page: this.data.page,
      },
      success: (res) => {
        let list = res.list;
        if (this.data.page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list: list,
          hasMore: list.length < Number(res.total),
          page: this.data.page + 1,
          isLoad: true,
        });
      },
    });
  },
  loadNav() {
    $util.request({
      url: 'entry/wxapp/edu.train.getlessoncate',
      data: {},
      success: (res) => {
        this.setData({
          nav: res.list,
          page: 1,
        }, () => {
          this.loadDetail();
        });
      },
    });
  },
  onReachBottom() {
    this.data.hasMore && this.loadDetail();
  },
  onShareAppMessage() {
    let _this = this;
    let path = '/ws_party_const/pages/education/train/network/category/index';
    let res = {
      path,
    };
    return res;
  },
});